<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-platform">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;进度管理添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="progressForm"
            :rules="rules"
            ref="progressForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目编号:" prop="projectId">
                <el-input v-model="progressForm.projectId" placeholder="请输入项目编号"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目名称:" prop="projectName">
                <el-input v-model="progressForm.projectName" placeholder="输入项目名称"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="负责人:" prop="name">
            <el-input v-model="progressForm.name" placeholder="输入负责人"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人电话:" prop="cell">
                <el-input v-model="progressForm.cell" placeholder="输入负责人电话"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="电子邮件:" prop="email">
                <el-input v-model="progressForm.email" placeholder="输入电子邮件"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="预期时间:" prop="expectedTime">
            <el-input v-model="progressForm.expectedTime" placeholder="输入预期时间"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目开始:" prop="start">
                <el-date-picker
                    v-model="progressForm.start"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择项目开始时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="项目结束:" prop="end">
                <el-date-picker
                    v-model="progressForm.end"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择项目结束时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否结束:" prop="checkOver">
                <el-input v-model="progressForm.checkOver" placeholder="输入是否结束"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="费用:" prop="cost">
                <el-input v-model="progressForm.cost" placeholder="输入费用"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('progressForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('progressForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progressForm: {
        projectId: 22101028,
        projectName: '达内时代2210班',
        name: '杰哥',
        cell: '17655370099',
        email: '1329709054@qq.com',
        expectedTime: '半年',
        start: '2021-06-01',
        end: '2022-1-24',
        checkOver: '是',
        cost: 1200,
      },
      rules: {
        start: [
          {required: true, message: "请选择项目开始时间", trigger: 'change'}
        ],
        end: [
          {required: true, message: "请选择项目结束时间", trigger: 'change'}
        ],
        projectName: [
          {required: true, message: "请输入项目名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        name: [
          {required: true, message: "请输入负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        checkOver: [
          {required: true, message: "是否结束", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        projectId: [
          {required: true, message: "请输入项目编号", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        email: [
          {required: true, message: "请输入电子邮件", trigger: 'blur'},
          {pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, message: "请输入正确的邮箱格式", trigger: 'blur'}
        ],
        cell: [
          {required: true, message: "请输入负责人电话", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        expectedTime: [
          {required: true, message: "请输入出库负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]|[0-9]+$/, message: "请输入中文和数字", trigger: 'blur'}
        ],
        cost: [
          {required: true, message: "请输入费用", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    /*添加*/
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/produceProgress/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.progressForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /*重置*/
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>